package com.earnings.okhttputils.utils.car.ui;

import android.content.DialogInterface;
import android.support.annotation.IdRes;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.earnings.R;
import com.earnings.okhttputils.utils.car.utils.StringUtils;
import com.earnings.okhttputils.utils.ui.BaseFragment;

/**
 * 贷款
 * Created by Administrator on 2017/6/20 0020.
 */

public class LoanFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, DialogInterface.OnCancelListener, TextWatcher {
    private String title, price, name;
    private TextView nakedcar_tv, must_tv, name_tv, nakedcar_tv2, purchase_tv2, clssj_tv,
            qcqdx_tv, zrrsx_tv, sanfangtv_tv, sbjmx_tv, wgzrx_tv, carbody_tv, boli_tv, must_tv2,
            insurance_tv, business_tv, shoufu_tv, yuegong_tv, lixi_tv, shoufukuan_tv, zongjia_tv, daikuane_tv;
    private EditText sanfanged_ed, carbody_ed, boli_ed, mymoney_ed;
    private ImageView img_view1, img_view2, img_view3, img_view4, img_view5, img_view6, img_view7, img_view8, img_view9;
    private ImageView imageViews[];
    private RadioGroup meal_group, shoufu_rg, huankuan_rg;
    private double iprice;
    private SanFangXianDialog mSanFangXianDialog;
    private CarBodyDialog mCarBodyDialog;
    private BoliDialog mBoliDialog;

    @Override
    protected void init() {
        nakedcar_tv = (TextView) mView.findViewById(R.id.nakedcar_tv);
        nakedcar_tv2 = (TextView) mView.findViewById(R.id.nakedcar_tv2);
        zongjia_tv = (TextView) mView.findViewById(R.id.zongjia_tv);
        shoufukuan_tv = (TextView) mView.findViewById(R.id.shoufukuan_tv);
        zrrsx_tv = (TextView) mView.findViewById(R.id.zrrsx_tv);
        shoufu_tv = (TextView) mView.findViewById(R.id.shoufu_tv);
        purchase_tv2 = (TextView) mView.findViewById(R.id.purchase_tv2);
        daikuane_tv = (TextView) mView.findViewById(R.id.daikuane_tv);
        yuegong_tv = (TextView) mView.findViewById(R.id.yuegong_tv);
        boli_tv = (TextView) mView.findViewById(R.id.boli_tv);
        carbody_tv = (TextView) mView.findViewById(R.id.carbody_tv);
        insurance_tv = (TextView) mView.findViewById(R.id.insurance_tv);
        business_tv = (TextView) mView.findViewById(R.id.business_tv);
        qcqdx_tv = (TextView) mView.findViewById(R.id.qcqdx_tv);
        lixi_tv = (TextView) mView.findViewById(R.id.lixi_tv);
        must_tv2 = (TextView) mView.findViewById(R.id.must_tv2);
        clssj_tv = (TextView) mView.findViewById(R.id.clssj_tv);
        must_tv = (TextView) mView.findViewById(R.id.must_tv);
        sanfangtv_tv = (TextView) mView.findViewById(R.id.sanfangtv_tv);
        wgzrx_tv = (TextView) mView.findViewById(R.id.wgzrx_tv);
        sbjmx_tv = (TextView) mView.findViewById(R.id.sbjmx_tv);
        sanfanged_ed = (EditText) mView.findViewById(R.id.sanfanged_ed);
        mymoney_ed = (EditText) mView.findViewById(R.id.mymoney_ed);
        boli_ed = (EditText) mView.findViewById(R.id.boli_ed);
        carbody_ed = (EditText) mView.findViewById(R.id.carbody_ed);
        name_tv = (TextView) mView.findViewById(R.id.name_tv);
        meal_group = (RadioGroup) mView.findViewById(R.id.meal_group);
        shoufu_rg = (RadioGroup) mView.findViewById(R.id.shoufu_rg);
        huankuan_rg = (RadioGroup) mView.findViewById(R.id.huankuan_rg);

        img_view1 = (ImageView) mView.findViewById(R.id.img_view1);
        img_view2 = (ImageView) mView.findViewById(R.id.img_view2);
        img_view3 = (ImageView) mView.findViewById(R.id.img_view3);
        img_view4 = (ImageView) mView.findViewById(R.id.img_view4);
        img_view5 = (ImageView) mView.findViewById(R.id.img_view5);
        img_view6 = (ImageView) mView.findViewById(R.id.img_view6);
        img_view7 = (ImageView) mView.findViewById(R.id.img_view7);
        img_view8 = (ImageView) mView.findViewById(R.id.img_view8);
        img_view9 = (ImageView) mView.findViewById(R.id.img_view9);
        imageViews = new ImageView[]{img_view1, img_view2, img_view3, img_view4, img_view5, img_view6, img_view7, img_view8, img_view9};

        mView.findViewById(R.id.sanfang_view).setOnClickListener(this);
        mView.findViewById(R.id.chexian_view).setOnClickListener(this);
        mView.findViewById(R.id.boli_view).setOnClickListener(this);
        meal_group.setOnCheckedChangeListener(this);
        shoufu_rg.setOnCheckedChangeListener(this);
        huankuan_rg.setOnCheckedChangeListener(this);
        mymoney_ed.addTextChangedListener(this);

        mSanFangXianDialog = new SanFangXianDialog(mContext);
        mCarBodyDialog = new CarBodyDialog(mContext);
        mBoliDialog = new BoliDialog(mContext);
        mSanFangXianDialog.setOnCancelListener(this);
        mCarBodyDialog.setOnCancelListener(this);
        mBoliDialog.setOnCancelListener(this);
        sanfanged_ed.setHint("保费5万");
        carbody_ed.setHint("保费2000");
        boli_ed.setHint("国产玻璃");
    }

    @Override
    public void onStart() {
        super.onStart();
        nakedcar_tv.setText(price);
        name_tv.setText(name);
        nakedcar_tv2.setText(price);
        purchase_tv2.setText(sgzs);
        clssj_tv.setText(szlssx);
        qcqdx_tv.setText(sqcqdx);
        zrrsx_tv.setText(szrssx);
        sbjmx_tv.setText(sbjmx);
        wgzrx_tv.setText(swgzrx);
        boli_tv.setText(sboli);
        must_tv2.setText(smust2);
        must_tv.setText(smust2);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        shoufu_tv.setText(sshoufu);
        shoufukuan_tv.setText(sshoufu);
        yuegong_tv.setText(syuegong);
        lixi_tv.setText(slixi);
        zongjia_tv.setText(szongjia);
        daikuane_tv.setText(sdaikuan);

    }

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_loan;
    }

    public String getPrice() {
        return price;
    }

    private double gzs, zlssx, qcqdx, zrssx, bjmx, boli, must2, insurance, total, shoufu = 0.3d, nianfen = 0.08553808d, nianfen2 = 12d, yuegong, lixi, zongjia, daikuan;
    private String sgzs, szlssx, sqcqdx, szrssx, sbjmx, swgzrx, sboli, smust2, sinsurance, stotal, sshoufu, syuegong, slixi, szongjia, sdaikuan;
    private int index, index2, index3;

    public void setPrice(String price, String name) {
        this.name = name;
        iprice = Double.parseDouble(price) * 10000;
        //裸车价格
        this.price = StringUtils.doublePrice2FromatPriceStr(iprice);
        //购置税(购车款/(1+17%)×购置税率(10%))
        gzs = (iprice / 1.17) * 0.1;
        gzs = Math.floor(gzs);
        sgzs = StringUtils.doublePrice2FromatPriceStr(gzs);
        //车辆损失险  基础保费(450)+裸车价格×1.0880%
        zlssx = 450 + iprice * 0.01088;
        zlssx = Math.floor(zlssx);
        szlssx = StringUtils.doublePrice2FromatPriceStr(zlssx);
        //全车盗抢险  基础保费(450)+裸车价格×费率(0.0032)
        qcqdx = 450 + iprice * 0.0032;
        sqcqdx = StringUtils.doublePrice2FromatPriceStr(qcqdx);
        //玻璃单独破碎险 进口新车购置价×0.25%，国产新车购置价×0.15%
        boli = iprice * 0.0015;
        sboli = StringUtils.doublePrice2FromatPriceStr(boli);
        //自燃损失险  新车购置价×0.15%
        zrssx = iprice * 0.0015;
        szrssx = StringUtils.doublePrice2FromatPriceStr(zrssx);
        //不计免赔特约险 (车辆损失险+第三者责任险)×20%
        bjmx = (zlssx + 516) * 0.2;
        sbjmx = StringUtils.doublePrice2FromatPriceStr(bjmx);
        //无过责任险  第三者责任险保险费×20%
        wgzrx = 516 * 0.2;
        swgzrx = StringUtils.doublePrice2FromatPriceStr(wgzrx);
        //车上人员责任险  每人保费50元(暂时先算一个)

        //    首期付款总额=首付款+必要花费+商业保险
        // 不好算   每期还款额＝贷款本金／还款期数＋（本金－已归还本金累计）×利率（月利率）

        //必要花费
        must2 = 500 + 300 + 950 + gzs;
        smust2 = StringUtils.doublePrice2FromatPriceStr(must2);
        //商业保险
        insurance = zlssx + qcqdx + boli + zrssx + bjmx + wgzrx + 50 + 400;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        //全款
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
        //首付
        double shoufu = iprice * this.shoufu;
        sshoufu = StringUtils.doublePrice2FromatPriceStr(shoufu);
        //月供
        yuegong = (iprice - shoufu) * nianfen;
        syuegong = StringUtils.doublePrice2FromatPriceStr(yuegong);
        //利息
        lixi = ((yuegong * nianfen2) + shoufu) - iprice;
        slixi = StringUtils.doublePrice2FromatPriceStr(lixi);
        //总价
        zongjia = iprice + lixi + insurance + must2;
        szongjia = StringUtils.doublePrice2FromatPriceStr(zongjia);
        //贷款额 贷款额=购车价格 - 首付款
        daikuan = iprice - shoufu;
        sdaikuan = StringUtils.doublePrice2FromatPriceStr(daikuan);
        //自定义最大金额
        maxprice = iprice * 0.6d;

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.sanfang_view) mSanFangXianDialog.show();
        if (v.getId() == R.id.chexian_view) mCarBodyDialog.show();
        if (v.getId() == R.id.boli_view) mBoliDialog.show();

    }

    @Override
    public String getTitle() {
        return title;
    }

    @Override
    public void setTitle(String title) {
        this.title = title;
    }

    private double number = 516, wgzrx, number2 = 400, number3;
    private int tag;

    //    首期付款总额=首付款+必要花费+商业保险
    // 不好算   每期还款额＝贷款本金／还款期数＋（本金－已归还本金累计）×利率（月利率）
    //12个月 平均每月每期还款额:0.08553808
    //24个月 平均每月每期还款额:0.04398616
    //36个月 平均每月每期还款额:0.03008549
    //48个月 平均每月每期还款额:0.02314168
    //60个月 平均每月每期还款额:0.01898453
    public void updata() {
        //首付
        double shoufu = iprice * this.shoufu;
        sshoufu = StringUtils.doublePrice2FromatPriceStr(shoufu);
        //月供
        yuegong = (iprice - shoufu) * nianfen;
        syuegong = StringUtils.doublePrice2FromatPriceStr(yuegong);
        //利息
        lixi = ((yuegong * nianfen2) + shoufu) - iprice;
        slixi = StringUtils.doublePrice2FromatPriceStr(lixi);
        //总价
        zongjia = iprice + lixi + insurance + must2;
        szongjia = StringUtils.doublePrice2FromatPriceStr(zongjia);
        //贷款额=购车价格 - 首付款
        daikuan = iprice - shoufu;
        sdaikuan = StringUtils.doublePrice2FromatPriceStr(daikuan);

        shoufu_tv.setText(sshoufu);
        shoufukuan_tv.setText(sshoufu);
        yuegong_tv.setText(syuegong);
        lixi_tv.setText(slixi);
        zongjia_tv.setText(szongjia);
        daikuane_tv.setText(sdaikuan);
    }

    public void updata(double shoufu) {
        sshoufu = StringUtils.doublePrice2FromatPriceStr(shoufu);
        //月供
        yuegong = (iprice - shoufu) * nianfen;
        syuegong = StringUtils.doublePrice2FromatPriceStr(yuegong);
        //利息
        lixi = ((yuegong * nianfen2) + shoufu) - iprice;
        slixi = StringUtils.doublePrice2FromatPriceStr(lixi);
        //总价
        zongjia = iprice + lixi + insurance + must2;
        szongjia = StringUtils.doublePrice2FromatPriceStr(zongjia);
        //贷款额=购车价格 - 首付款
        daikuan = iprice - shoufu;
        sdaikuan = StringUtils.doublePrice2FromatPriceStr(daikuan);

        shoufu_tv.setText(sshoufu);
        shoufukuan_tv.setText(sshoufu);
        yuegong_tv.setText(syuegong);
        lixi_tv.setText(slixi);
        zongjia_tv.setText(szongjia);
        daikuane_tv.setText(sdaikuan);
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        if (group.getId() == R.id.meal_group) {
            for (int i = 0; i < group.getChildCount(); i++) {
                RadioButton radiobutton = (RadioButton) group.getChildAt(i);
                radiobutton.setBackgroundResource(R.drawable.meal_bg);
            }
            RadioButton radiobutton = (RadioButton) group.findViewById(checkedId);
            radiobutton.setBackgroundResource(R.drawable.meal_bg2);
            //全险
            if (checkedId == R.id.quanxian) {
                tag = 0;
                for (int i = 0; i < imageViews.length; i++) {
                    imageViews[i].setImageResource(R.mipmap.cloose_hit);
                }
                zongjia();
            }
            //经济险
            if (checkedId == R.id.jingjixian) {
                tag = 1;
                for (int i = 0; i < imageViews.length; i++) {
                    imageViews[i].setImageResource(R.mipmap.cloose_hit);
                }
                img_view3.setImageResource(R.mipmap.choose);
                img_view4.setImageResource(R.mipmap.choose);
                img_view5.setImageResource(R.mipmap.choose);
                img_view9.setImageResource(R.mipmap.choose);
                jingjixian();
            }

            //基本险
            if (checkedId == R.id.jibenxian) {
                tag = 2;
                for (int i = 0; i < imageViews.length; i++) {
                    imageViews[i].setImageResource(R.mipmap.cloose_hit);
                }
                img_view4.setImageResource(R.mipmap.choose);
                img_view5.setImageResource(R.mipmap.choose);
                img_view6.setImageResource(R.mipmap.choose);
                img_view7.setImageResource(R.mipmap.choose);
                img_view9.setImageResource(R.mipmap.choose);
                jibenxian();
            }
        }
        //首付额度
        if (group.getId() == R.id.shoufu_rg) {
            for (int i = 0; i < group.getChildCount(); i++) {
                RadioButton radiobutton = (RadioButton) group.getChildAt(i);
                radiobutton.setBackgroundResource(R.drawable.meal_bg);
            }
            RadioButton radiobutton = (RadioButton) group.findViewById(checkedId);
            radiobutton.setBackgroundResource(R.drawable.meal_bg2);
            if (checkedId == R.id.sanshi) shoufu = 0.3d;
            if (checkedId == R.id.sishi) shoufu = 0.4d;
            if (checkedId == R.id.wushi) shoufu = 0.5d;
            if (checkedId == R.id.liushi) shoufu = 0.6d;
            updata();
        }
        //还款年份
        if (group.getId() == R.id.huankuan_rg) {
            for (int i = 0; i < group.getChildCount(); i++) {
                RadioButton radiobutton = (RadioButton) group.getChildAt(i);
                radiobutton.setBackgroundResource(R.drawable.meal_bg);
            }
            RadioButton radiobutton = (RadioButton) group.findViewById(checkedId);
            radiobutton.setBackgroundResource(R.drawable.meal_bg2);
            //12个月 平均每月每期还款额:0.08553808
            //24个月 平均每月每期还款额:0.04398616
            //36个月 平均每月每期还款额:0.03008549
            //48个月 平均每月每期还款额:0.02314168
            //60个月 平均每月每期还款额:0.01898453
            if (checkedId == R.id.yinian_tv) {
                nianfen2 = 12d;
                nianfen = 0.08553808d;
            }
            if (checkedId == R.id.liangnian_tv) {
                nianfen2 = 24d;
                nianfen = 0.04398616d;
            }
            if (checkedId == R.id.sannian_tv) {
                nianfen2 = 36d;
                nianfen = 0.03008549d;
            }
            if (checkedId == R.id.sinian_tv) {
                nianfen2 = 48d;
                nianfen = 0.02314168d;
            }
            if (checkedId == R.id.wunian_tv) {
                nianfen2 = 60d;
                nianfen = 0.01898453d;
            }
            updata();

        }


    }

    @Override
    public void onCancel(DialogInterface dialog) {
        if (dialog instanceof SanFangXianDialog) {
            //第三方责任险
            index = mSanFangXianDialog.index;
            String text = null;
            switch (index) {
                case 0:
                    number = 516;
                    break;
                case 1:
                    number = 746;
                    break;
                case 2:
                    number = 924;
                    break;
                case 3:
                    number = 1252;
                    break;
                case 4:
                    number = 1630;
                    break;
            }
            text = StringUtils.doublePrice2FromatPriceStr(number);
            sanfangtv_tv.setText(text);
            sanfanged_ed.setHint(mSanFangXianDialog.text);
            //不计免赔特约险 (车辆损失险+第三者责任险)×20%
            bjmx = (zlssx + number) * 0.2;
            sbjmx = StringUtils.doublePrice2FromatPriceStr(bjmx);
            sbjmx_tv.setText(sbjmx);
            //无过责任险  第三者责任险保险费×20%
            wgzrx = number * 0.2;
            swgzrx = StringUtils.doublePrice2FromatPriceStr(wgzrx);
            wgzrx_tv.setText(swgzrx);
        }
        if (dialog instanceof CarBodyDialog) {
            carbody_ed.setHint(mCarBodyDialog.text);
            index2 = mCarBodyDialog.index;
            String text = null;
            switch (index2) {
                case 0:
                    number2 = 400;
                    break;
                case 1:
                    number2 = 570;
                    break;
                case 2:
                    number2 = 760;
                    break;
                case 3:
                    number2 = 1140;
                    break;
            }
            //车身划痕险
            text = StringUtils.doublePrice2FromatPriceStr(number2);
            carbody_tv.setText(text);
        }
        if (dialog instanceof BoliDialog) {
            boli_ed.setHint(mBoliDialog.text);
            index3 = mBoliDialog.index;
            String text = null;
            switch (index3) {
                case 0:
                    number3 = iprice * 0.0015;
                    break;
                case 1:
                    number3 = iprice * 0.0025;
                    break;
            }

            //玻璃单独破碎险 进口新车购置价×0.25%，国产新车购置价×0.15%
            text = StringUtils.doublePrice2FromatPriceStr(number3);
            boli_tv.setText(text);
        }

        switch (tag) {
            case 0:
                zongjia();
                break;
            case 1:
                jingjixian();
                break;
            case 2:
                jibenxian();
                break;
        }
        updata();
    }

    private void zongjia() {
        //商业保险
        insurance = zlssx + qcqdx + boli + zrssx + bjmx + wgzrx + 50 + number2;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        //总价
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
    }

    //基本险
    private void jibenxian() {
        //商业险
        insurance = 50 + number + qcqdx + zlssx;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        //总价
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
    }

    //经济险
    private void jingjixian() {
        //商业险
        insurance = number + 50 + zlssx + bjmx + wgzrx;
        sinsurance = StringUtils.doublePrice2FromatPriceStr(insurance);
        insurance_tv.setText(sinsurance);
        business_tv.setText(sinsurance);
        //总价
        total = insurance + must2 + iprice;
        stotal = StringUtils.doublePrice2FromatPriceStr(total);
    }

    private double maxprice, importprice;
    private boolean isimport = true;
    private int editStart, editEnd;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!s.toString().equals("")) {
            double price = Double.parseDouble(s.toString());
            if (price > maxprice) {
                isimport = false;
                Toast.makeText(mContext, "金额最大为裸车的60%！", Toast.LENGTH_LONG).show();
                importprice = maxprice;
            } else {
                isimport = true;
                importprice = price;
            }
        }

    }

    @Override
    public void afterTextChanged(Editable s) {
        editStart = mymoney_ed.getSelectionStart();
        editEnd = mymoney_ed.getSelectionEnd();
        if (!isimport) {
            s.delete(editStart - 1, editEnd);
            int tempSelection = editStart;
            mymoney_ed.setText(s);
            mymoney_ed.setSelection(tempSelection);
        }
        updata(importprice);
    }
}
